import {
  getBatchInfo,
  getBatchQuestionInfo,
  getQiniuToken,
  getUserAnswer,
  saveUserAnswer,
  deleteUserAnswer
} from '@/api'
import { Reducer, Effect, Subscription } from 'umi'
import { ResultType } from '@/types/index'
interface EvaluationType {
  code: string
  content: string
  createTime: string
  criteriaName: string
  desc: string
  grade: string
  name: string
  publishEndDate: string
  publishStartDate: string
  reviewEndDate: string
  reviewStartDate: string
  status: number
  unit: string
}
interface TopicType {
  code: string
  title: string
  name: string
  type: string | any[]
}
interface UploadFileType {
  desc: string
  fileName: string
  fileSize: string
  questionCode: string
  type: string
  uploadTime: string
  url: string
  userCode: string
}
interface PointInfoType {
  name: string
  code: string
  point: string
  children: PointInfoType[]
}
export interface SchoolUserStateType {
  evaluationList: EvaluationType[]
  topicList: TopicType[]
  qiniuToken: string
  uploadFileList: UploadFileType[]
  pointInfo: PointInfoType[]
}
interface SchoolUserModelType {
  namespace: string
  state: SchoolUserStateType
  reducers: {
    setState: Reducer<SchoolUserStateType>
  }
  effects: {
    function_getBatchInfo: Effect
    function_getBatchQuestionInfo: Effect
    function_getQiniuToken: Effect
    function_getUserAnswer: Effect
    function_saveUserAnswer: Effect
    function_deleteUserAnswer: Effect
  }
  subscriptions: {
    setup: Subscription
  }
}

export default <SchoolUserModelType>{
  namespace: 'schoolUserModel',
  state: {
    evaluationList: [],
    topicList: [],
    qiniuToken: '',
    uploadFileList: [],
    pointInfo: []
  },
  reducers: {
    setState(state, { payload }) {
      return { ...state, ...payload }
    }
  },
  effects: {
    // 获取管理端评价信息
    *function_getBatchInfo({}, { call, put }) {
      const result: ResultType = yield call(getBatchInfo)
      if (result.code === 200) {
        yield put({
          type: 'setState',
          payload: {
            evaluationList: result.obj
          }
        })
      }
    },
    // 查询评价批次题目信息
    *function_getBatchQuestionInfo(
      { payload: { batchCode, type } },
      { call, put }
    ) {
      const result: ResultType = yield call(
        getBatchQuestionInfo,
        batchCode,
        type
      )
      if (result.code === 200) {
        yield put({
          type: 'setState',
          payload: {
            topicList: result.obj
          }
        })
      }
    },
    // 获取七牛token
    *function_getQiniuToken({}, { call, put }) {
      const result: ResultType = yield call(getQiniuToken)
      if (result.code === 200) {
        yield put({
          type: 'setState',
          payload: {
            qiniuToken: (result.obj as { token: string }).token
          }
        })
      }
    },
    // 查询用户题目上传信息
    *function_getUserAnswer(
      { payload: { batchCode, userCode } },
      { call, put }
    ) {
      const result: ResultType = yield call(getUserAnswer, batchCode, userCode)
      if (result.code === 200) {
        yield put({
          type: 'setState',
          payload: {
            uploadFileList: result.obj
          }
        })
      }
    },
    // 保存题目用户上传信息
    *function_saveUserAnswer(
      { payload: { batchCode, code, type, url, userCode, fileSize }, callback },
      { call }
    ) {
      const result: ResultType = yield call(
        saveUserAnswer,
        batchCode,
        code,
        type,
        url,
        userCode,
        fileSize
      )
      callback(result)
    },
    // 删除用户上传的资料信息
    *function_deleteUserAnswer(
      { payload: { batchCode, code, type, userCode, fileName }, callback },
      { call }
    ) {
      const result: ResultType = yield call(
        deleteUserAnswer,
        batchCode,
        code,
        type,
        userCode,
        fileName
      )
      callback(result)
    }
  },
  subscriptions: {
    setup(dispatch, history) {}
  }
}
